package com.maiji.cloud.controller;

import com.maiji.cloud.request.BaseDataReqDto;
import com.maiji.cloud.request.shopingmall.AddToShopingCartReqDto;
import com.maiji.cloud.request.shopingmall.ShopingCartCheckReqData;
import com.maiji.cloud.request.shopingmall.ShopingCartDelReqDto;
import com.maiji.cloud.request.shopingmall.ShopingCartQuantityUpdateReqData;
import com.maiji.cloud.response.BaseDataResDto;
import com.maiji.cloud.response.BaseMetaResDto;
import com.maiji.cloud.response.BaseResDto;
import com.maiji.cloud.response.Status;
import com.maiji.cloud.response.shopingmall.ShopingCartChosenResData;
import com.maiji.cloud.response.shopingmall.ShopingCartResData;
import com.maiji.cloud.service.ShopingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Arrays;
import java.util.List;

/**
 *  购物车相关操作
 */
@RestController
@RequestMapping("/shopingCart")
public class ShopingCartController {

    @Autowired
    ShopingCartService shopingCartService;

    /**
     *  商品批量加入购物车
     * @param baseDataReqDto
     * @param token
     * @return
     */
    @PostMapping("addBatchToShopingCart")
    public BaseResDto addBatchToShopingCart(@RequestBody BaseDataReqDto<List<String>> baseDataReqDto,
                                       @RequestHeader("maijiToken") String token) {
        List<String> goodsIds = baseDataReqDto.getData();
        if (goodsIds == null || goodsIds.size() == 0) return new BaseResDto(Status.PARAMETERERROR);
        return shopingCartService.addToShopingCart(goodsIds, 1, token);
    }

    /**
     *  商品加入购物车
     * @param addToShopingCartReqDto
     * @param token
     * @return
     */
    @PostMapping("addToShopingCart")
    public BaseResDto addToShopingCart(@RequestBody AddToShopingCartReqDto addToShopingCartReqDto,
                                       @RequestHeader("maijiToken") String token) {
        String goodsId = addToShopingCartReqDto.getData().getGoodsId();
        Integer quantity = addToShopingCartReqDto.getData().getQuantity();
        if (goodsId == null || quantity == null) return new BaseResDto(Status.PARAMETERERROR);
        return shopingCartService.addToShopingCart(Arrays.asList(goodsId), quantity, token);
    }

    /**
     *  获取购物车商品列表
     * @return
     */
    @PostMapping("findAllShopingCart")
    public BaseMetaResDto<List<ShopingCartResData>> FindAllShopingCart(@RequestHeader("maijiToken") String token) {
        return shopingCartService.FindAllShopingCart(token);
    }

    /**
     *  获取购物车商品列表
     * @return
     */
    @PostMapping("findUserShopingCartCount")
    public BaseDataResDto<Integer> findUserShopingCartCount(@RequestHeader("maijiToken") String token) {
        return shopingCartService.findUserShopingCartCount(token);
    }

    /**
     *  选择购物车商品（全选）
     * @param baseDataReqDto
     * @return
     */
    @PostMapping("checkShopingCart")
    public BaseDataResDto<ShopingCartChosenResData> checkShopingCart(@RequestBody BaseDataReqDto<ShopingCartCheckReqData> baseDataReqDto,
                                                                     @RequestHeader("maijiToken") String token) {
        ShopingCartCheckReqData data = baseDataReqDto.getData();
        return shopingCartService.chosenShopingCart(data, token);
    }

    /**
     *  增加或减少购物车商品数量
     * @param baseDataReqDto
     * @return
     */
    @PostMapping("updateShopingCartQuantity")
    public BaseResDto updateShopingCartQuantity(@RequestBody BaseDataReqDto<ShopingCartQuantityUpdateReqData> baseDataReqDto) {
        String shopingCartId = baseDataReqDto.getData().getUuId();
        Integer purchaseQuantity = baseDataReqDto.getData().getPurchaseQuantity();
        if (shopingCartId == null || purchaseQuantity == null || purchaseQuantity == 0)
            return new BaseMetaResDto(Status.PARAMETERERROR);
        return shopingCartService.updateShopingCartQuantity(shopingCartId, purchaseQuantity);
    }

    /**
     *  批量删除购物车商品
     * @param shopingCartDelReqDto
     * @return
     */
    @PostMapping("delShopingCart")
    public BaseResDto delShopingCart(@RequestBody ShopingCartDelReqDto shopingCartDelReqDto) {
        List<String> shopingCartIds = shopingCartDelReqDto.getData();
        if (shopingCartIds == null) return new BaseMetaResDto(Status.PARAMETERERROR);
        return shopingCartService.delShopingCart(shopingCartIds);
    }

}

